package com.pshcp.patient.api;

import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.common.config.CustomConf;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.BannerBizStatusEnum;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.BannerDTO;
import com.pshcp.domain.model.Banner;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.OrderReg;
import com.pshcp.domain.model.PatientOnlineOrder;
import com.pshcp.domain.vo.BannerVO;
import com.pshcp.patient.domain.home.vo.ActiveOrderVO;
import com.pshcp.patient.tool.PatientHomeTools;
import com.pshcp.service.BannerService;
import com.pshcp.service.DoctorService;
import com.pshcp.service.OrderRegService;
import com.pshcp.service.PatientFavorService;
import com.pshcp.service.PatientOnlineOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = {"患者端-首页"})
@Slf4j
@RestController
@RequestMapping("/api/patient/home")
public class PatientHomeAPI {

    @Autowired
    private CustomConf customConf;

    @Autowired
    private BannerService bannerService;

    @Autowired
    private PatientOnlineOrderService onlineOrderService;

    @Autowired
    private OrderRegService orderRegService;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private PatientFavorService favorService;

    @ApiOperation("banner图")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "banner图")
    @PostMapping("/bannerList")
    public ResultDTO<List<BannerVO>> bannerList() {
        BannerDTO bannerDTO = new BannerDTO();
        bannerDTO.setBizStatus(BannerBizStatusEnum.SHOW.getCode());
        bannerDTO.setStatus(CommonStatusEnum.INIT.getCode());
        List<Banner> bannerList = bannerService.list(bannerDTO);
        if(!CollectionUtils.isEmpty(bannerList)){
            bannerList.forEach(b -> {
                b.setPath(customConf.getFileUrl() + b.getPath());
            });
        }
        return ResultBuilder.success(CommonTool.copyPropertiesList(bannerList, BannerVO.class));
    }

    @ApiOperation("实时订单情况")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "实时订单情况")
    @PostMapping("/activeOrder")
    public ResultDTO<ActiveOrderVO> activeOrder() {
        ActiveOrderVO activeOrderVO = new ActiveOrderVO();
        //进行中在线订单
        List<PatientOnlineOrder> activeOnlineOrder = onlineOrderService.activeOnlineOrder();
        if(!CollectionUtils.isEmpty(activeOnlineOrder)){
            PatientHomeTools.buildActiveOnline(activeOrderVO, activeOnlineOrder);
        }
        //最近问诊医生
        PatientOnlineOrder lastOnlineOrder = onlineOrderService.lastOnlineOrder();
        if(lastOnlineOrder != null){
            Doctor doctor = doctorService.findById(lastOnlineOrder.getDoctorId());
            boolean isFavor = favorService.patientUserIsFavor(lastOnlineOrder.getDoctorId());
            PatientHomeTools.buildActiveDoctor(activeOrderVO, doctor, isFavor, customConf.getFileUrl());
        }
        //未出诊挂号单
        List<OrderReg> orderRegs = orderRegService.waitRegOrder();
        if(!CollectionUtils.isEmpty(orderRegs)){
            PatientHomeTools.buildActiveRegOrder(activeOrderVO, orderRegs);
        }
        return ResultBuilder.success(activeOrderVO);
    }

}
